package com.xy.tianyu.module.novel

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.Color
import android.view.View
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.FragmentUtils
import com.xy.tianyu.BaseActivity
import com.xy.tianyu.R
import com.xy.tianyu.module.novel.fragment.BookStackRoomFragment
import com.xy.tianyu.module.novel.fragment.BookShelfFragment
import com.xy.tianyu.module.novel.fragment.BookCompetitiveFragment
import kotlinx.android.synthetic.main.activity_book.*


/**
 * @time 2020/10/27 14:39
 * @class 描述:
 */
class BookActivity : BaseActivity() {
    private lateinit var bookShelfFragment: BookShelfFragment
    private lateinit var bookCompetitiveFragment: BookCompetitiveFragment
    private lateinit var bookStackRoomFragment: BookStackRoomFragment
    private var selectColor = Color.parseColor("#FFA428")
    private var normalColor = Color.parseColor("#000000")

    override fun getLayoutId(): Int {
        return R.layout.activity_book
    }

    override fun initView() {
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this,R.color.white))
        bookShelfFragment = BookShelfFragment()
        bookCompetitiveFragment = BookCompetitiveFragment()
        bookStackRoomFragment = BookStackRoomFragment()
        FragmentUtils.add(supportFragmentManager,bookShelfFragment,R.id.fr_book_container,true)
        FragmentUtils.add(supportFragmentManager,bookCompetitiveFragment,R.id.fr_book_container,true)
        FragmentUtils.add(supportFragmentManager,bookStackRoomFragment,R.id.fr_book_container,true)

        bookStoreSelect(false)

        ll_book_shelf.setOnClickListener {
            bookShelfSelect()
        }

        ll_book_competitive.setOnClickListener {
            bookStoreSelect(true)
        }

        ll_book_stackroom.setOnClickListener {
            bookRankingSelect()
        }
    }

    /**
     * 选中书架
     */
    private fun bookShelfSelect(){

        animationScale(ll_book_shelf)

        iv_book_shelf.setImageResource(R.drawable.tab_icon_bookshelf_pressed)
        iv_book_competitive.setImageResource(R.drawable.tab_icon_competitive)
        iv_book_stackroom.setImageResource(R.drawable.tab_icon_stackroom)

        tv_book_shelf.setTextColor(selectColor)
        tv_book_competitive.setTextColor(normalColor)
        tv_book_stackroom.setTextColor(normalColor)

        FragmentUtils.showHide(bookShelfFragment,bookCompetitiveFragment,bookStackRoomFragment)
    }

    /**
     * 选中精品
     */
    private fun bookStoreSelect(showAnim:Boolean){

        if (showAnim) animationScale(ll_book_competitive)

        iv_book_shelf.setImageResource(R.drawable.tab_icon_bookshelf)
        iv_book_competitive.setImageResource(R.drawable.tab_icon_competitive_pressed)
        iv_book_stackroom.setImageResource(R.drawable.tab_icon_stackroom)

        tv_book_shelf.setTextColor(normalColor)
        tv_book_competitive.setTextColor(selectColor)
        tv_book_stackroom.setTextColor(normalColor)

        FragmentUtils.showHide(bookCompetitiveFragment,bookShelfFragment,bookStackRoomFragment)
    }

    /**
     * 选中书库
     */
    private fun bookRankingSelect(){

        animationScale(ll_book_stackroom)

        iv_book_shelf.setImageResource(R.drawable.tab_icon_bookshelf)
        iv_book_competitive.setImageResource(R.drawable.tab_icon_competitive)
        iv_book_stackroom.setImageResource(R.drawable.tab_icon_stackroom_pressed)

        tv_book_shelf.setTextColor(normalColor)
        tv_book_competitive.setTextColor(normalColor)
        tv_book_stackroom.setTextColor(selectColor)

        FragmentUtils.showHide(bookStackRoomFragment,bookCompetitiveFragment,bookShelfFragment)
    }

    private fun animationScale(v: View) {
        val animatorX = ObjectAnimator.ofFloat(v, "ScaleX", 1.0f, 0.6f, 1.2f, 1.0f)
        val animatorY = ObjectAnimator.ofFloat(v, "ScaleY", 1.0f, 0.6f, 1.2f, 1.0f)
        val animatorSet = AnimatorSet()
        animatorSet.playTogether(animatorX, animatorY)
        animatorSet.duration = 200
        animatorSet.start()
    }

}